package com.chinasoftinc.facommon.view.login.abilitypresent;

import ohos.aafwk.content.Intent;
import ohos.agp.utils.TextTool;
import ohos.bundle.ElementName;

import com.chinasoftinc.facommon.FAConfigMgr;
import com.chinasoftinc.facommon.utils.LogUtil;
import com.chinasoftinc.facommon.view.login.LoginAbility;

/**
 * @since 2022-05-09
 */
public class WifiLoginAbilityPresent extends LoginAbilityPresent {
    // 配网完成
    private static final int SOFTAP_NET_CONFIG_COMPLETE = 1;
    // 配网中
    private static final int SOFTAP_NET_CONFIGURING = 0;

    private static final int NET_CONFIG_COMPLETED = 1;

    public WifiLoginAbilityPresent(LoginAbility ability) {
        super(ability);
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        String nanSessionId = intent.getStringParam("nanSessionId");
        String sourceFA = intent.getStringParam("source");
        // fastcontrol字段的值来区分秒控和普通控制；秒控：true；普通控制：false
        boolean isFastControl = intent.getBooleanParam("fastControl", false);
        // businessStage字段用于标识业务进行的阶段，该字段值为1的时候，表示FA调用HiLinkSvc的SoftAP配网流程完成后回拉FA的阶段
        int businessStage = intent.getIntParam("businessStage", SOFTAP_NET_CONFIGURING);

        LogUtil.info(TAG, "onStart nanSessionId: " + nanSessionId);
        LogUtil.info(TAG, "onStart source: " + sourceFA);
        LogUtil.info(TAG, "onStart fastControl: " + isFastControl);
        LogUtil.info(TAG, "onStart businessStage: " + businessStage);

        verifyNetConfigProcess(isFastControl, businessStage, nanSessionId);
        configPageParams();
    }

    @Override
    public void startAbility() {
    }

    /**
     * 校验是否处于配网流程中
     *
     * @param fastControl nan配网状态值
     * @param businessStage softAp配网状态值
     * @param nanSessionId nan会话id
     * @noinspection UnusedReturnValue
     */
    public boolean verifyNetConfigProcess(boolean fastControl, int businessStage, String nanSessionId) {
        if (fastControl && isSessionIdValid(nanSessionId)) {
            openDevicePage(KEY_SESSION_ID, nanSessionId);
            return true;
            // NAN配网过程中，或者SoftAP配网结束
        } else //noinspection ConstantConditions
            if (businessStage == NET_CONFIG_COMPLETED && !TextTool.isNullOrEmpty(deviceId)) {
                openDevicePage(KEY_DEVICE_ID, deviceId);
                return true;
                // NAN配网过程中，或者SoftAP配网结束
            }
        // 首次碰一碰进入
        return false;
    }

    private static boolean isSessionIdValid(String sessionId) {
        return !TextTool.isNullOrEmpty(sessionId) && !"NAN_DEVICE_NOT_FOUND".equals(sessionId);
    }

    private void openDevicePage(String paramKey, String paramValue) {
        ability.getUITaskDispatcher().asyncDispatch(() -> {
            Intent intent = new Intent();
            intent.setParam(paramKey, paramValue);
            intent.setParam(KEY_PRODUCT_ID, productId);
            intent.setParam(KEY_UUID, uuid);
            ElementName elementName = new ElementName("", FAConfigMgr.get().getBundleName(), FAConfigMgr.get().getDeviceAbilityName());
            intent.setElement(elementName);
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION | Intent.FLAG_INSTALL_ON_DEMAND);
            ability.startAbility(intent, -1);
        });
    }
}
